package com.ABdolphin.forum.services.impl;

import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.dao.BoardMapper;
import com.ABdolphin.forum.exception.ApplicationException;
import com.ABdolphin.forum.model.Board;
import com.ABdolphin.forum.services.IBoardService;
import com.ABdolphin.forum.utils.StringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;

    @Override
    public void create(Board board) {
        if (board == null
                || StringUtil.isEmpty(board.getName())
                || board.getSort() == null) {
            log.info("参数检验异常, board为空或board属性为空");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board isExistBoard = boardMapper.selectByName(board.getName());
        //是否存在该板块
        if (isExistBoard!=null){
            log.info(ResultCode.FAILED_BOARD_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_EXISTS));
        }
        board.setArticleCount(0);
        board.setState((byte)0);
        board.setDeleteState((byte)0);
        board.setCreateTime(new Date());
        board.setUpdateTime(new Date());
        int result = boardMapper.insertSelective(board);
        if(result<1){
            log.error("数据库插入新版块失败");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_CREATE));
        }

    }

    @Override
    public Board selectById(Long id) {
        if (id==null){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

    @Override
    public List<Board> selectByNum(Integer num) {
        if (num==null||num<0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if (id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board==null){
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount()+1);
        updateBoard.setUpdateTime(new Date());
        int result = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (result<=0){
            log.warn(ResultCode.FAILED.toString() + ", 受影响行数不等于1,");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        if (id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board==null){
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if (updateBoard.getArticleCount()<0){
            updateBoard.setArticleCount(0);
        }
        updateBoard.setUpdateTime(new Date());
        int result = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (result<=0){
            log.warn(ResultCode.FAILED.toString() + ", 受影响行数不等于1,");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
    }
}
